package cn.juc;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 饥饿线程的解决
 * 线程池的专用职责
 */
@Slf4j
public class HangryThreadPool {

    public static void main(String[] args) throws InterruptedException {
        //职责分化
        ExecutorService waiter= Executors.newFixedThreadPool(1);
        ExecutorService cook= Executors.newFixedThreadPool(1);
         //log.info("xx");
        waiter.execute(()->{
            log.info("处理点餐");
            Future<?> submit = cook.submit(() -> {
                log.info("做饭中");
                return "饭做好了";
            });
            Object str = null;
            try {
                str = submit.get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
            log.info((String) str);
        });
        waiter.execute(()->{
            log.info("处理点餐");
            Future<?> submit = cook.submit(() -> {
                log.info("做饭中");
                return "饭做好了";
            });
            Object str = null;
            try {
                str = submit.get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
            log.info((String) str);
        });
        Thread.sleep(10000);
        waiter.shutdown();
        cook.shutdown();
    }
}
